package com.bbcare.followup.plat.service.impl;

import com.bbcare.comm.Constants;
import com.bbcare.followup.plat.vo.FuClosecaseRecordVo;
import com.bbcare.followup.plat.entity.FuClosecaseRecord;
import com.bbcare.followup.plat.service.FuClosecaseRecordService;
import com.bbcare.followup.plat.service.IPatientService;
import com.bbcare.followup.plat.store.ibatis.FuClosecaseRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title		FuClosecaseRecordServiceImpl.java
 * @Package		com.bbcare.followup.plat.service.impl
 * @Description	患者结案记录
 * @Author		<a href="mailto:jiangqian_java@163.com">jiang</a>
 * @Date		2022/6/17
 * @Version		V1.0 
 * @Copyright	(c) Shanghai BBcare Medical Equipment Co.,Ltd.
 */
@Service
public class FuClosecaseRecordServiceImpl implements FuClosecaseRecordService {

    @Resource(name = "jedisPool")
    private JedisPool jedisPool;

    @Resource
    private FuClosecaseRecordDao fuClosecaseRecordDao;

    @Autowired
    private IPatientService patientSerivce;


    @Override
    public Object insert(FuClosecaseRecord fuClosecaseRecord) {
        return fuClosecaseRecordDao.insert(fuClosecaseRecord);
    }


    @Override
    public Object delete(int id) {
        return fuClosecaseRecordDao.delete(id);
    }


    @Override
    public Object update(FuClosecaseRecord fuClosecaseRecord) {
        return fuClosecaseRecordDao.update(fuClosecaseRecord);
    }


    @Override
    public FuClosecaseRecord load(int id) {
        return fuClosecaseRecordDao.load(id);
    }


    @Override
    public HashMap<String, Object> pageList(Map<String, Object> paramMap) {
        HashMap<String, Object> retMap = new HashMap<String, Object>();
        Map<String, Object> qryMapOut = new HashMap<String, Object>();

        // 获取队列名称
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.select(Constants.REDIS_DB_INDEX_CONFIG);
            List<FuClosecaseRecordVo> records = fuClosecaseRecordDao.pageList(paramMap);
            Map<String, Object> qryMapIn = new HashMap<String, Object>();
            for (int i = 0; i < records.size(); i++) {
                FuClosecaseRecordVo vo = records.get(i);
                qryMapIn.put("queueId", vo.getQueueId());
                qryMapOut = patientSerivce.getQueueName(qryMapIn, jedis);
                vo.setQueueName((String) qryMapOut.get("queueName"));

            }

            retMap.put("total", fuClosecaseRecordDao.pageListCount(0,0));
            retMap.put("rows", fuClosecaseRecordDao.pageList(paramMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retMap;
    }

    public int countByUserid(final String userId)  {
        Map<String,Object> paraMap  = new HashMap<>();
        paraMap.put("userId",   userId);
        return fuClosecaseRecordDao.countByUserid(paraMap);
    }
}
